(function(){
    function GetPeriodInfo(){
        this.init(); 
    }
    GetPeriodInfo.prototype = {
        constructor:GetPeriodInfo,
        init:function(){
            this.nodata = '<tr><td  colspan="8">暂无信息</td></tr>';
            var _this = this;
        
                this.getReportList(1);
            
                $("#searchBtn").unbind("click").click(function(){
                     _this.getReportList(1);
                });

                $.extend({page:function(pn){
                    _this.getReportList(pn);
                }});
         
        },
        getReportList:function( pn ){
            var _this = this;
            var gameId = $("#gameid").val();
            var state = $("#tzstatus").val();
            state = state ?'&state='+state:'';
            var periodId = $("#periodId").val()+"";
            periodId = periodId ? "&period="+periodId : '';
            if( periodId){state='';}

            $.ajax({
                type:"get",
                url:"/admin/period?ps=20&pn="+pn+"&gameid="+gameId+state+periodId,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        var data = info.data;
                        var count = info.count;
                        var pageno = count.pn;
                       
                        
                        if( data.length )
                        {
                        //   编辑状态的时候只能由0 变为1  不能由其他状态变为0
                            var stateJson = {
                                "0":"未审核",
                                "1":"已审核",
                                "2":"已计奖"
                            };
                            var gameJson = {
                                "9":"广东11x5",
                                "10":"湖北11x5",
                                "1":"超级大乐透"
                            };
                            var strArr = [];
                            data.forEach(function( value, key  ){
                                var gameid = value.gameid;//彩种编号
                                var period = value.period;//
                                var stime = value.stime.substring(0,16);//开售时间
                                var etime = value.etime.substring(0,16);//截止时间
                                var oldvalue = value.oldvalue;//开奖号码
                                var newvalue = value.newvalue;//开奖奖金
                                var state = value.state;
                                var optbtn = '';
                                if( state==0 )
                                {
                                     optbtn = '<a href="javascript:;" class="btn btn-info  editorBtn" data-kstate="'+state+'" data-kjcode="'+oldvalue+'" data-kjmoney="'+newvalue+'"   data-kjperiod="'+period+'"    data-pageno="'+pageno+'">编辑</a>';
                                }

                                var trClass =  key%2 == 0 ? "even" : "odd";
                                var html = '<tr class="'+trClass+'"  >\
                                                <td>'+gameJson[gameid]+'</td>\
                                                <td>'+period+'</td>\
                                                <td>'+stime+'</td>\
                                                <td>'+etime+'</td>\
                                                <td>'+oldvalue+'</td>\
                                                <td>'+newvalue+'</td>\
                                                <td>'+stateJson[state]+'</td>\
                                                <td>'+optbtn+'</td>\
                                            </tr>';

                                    strArr.push( html );

                            });

                            $("#unticketList").html( strArr.join(""));
                            // 编辑
                            // 编辑用户
                            $(".editorBtn").unbind("click").click(function(){
                                var kjperiod = $(this).data("kjperiod");
                                var kjcode = $(this).data("kjcode");
                                var kjmoney = $(this).data("kjmoney");
                                var kstate = $(this).data("kstate");
                                
                                _this.pagenum = $(this).data("pageno");
                                $("#editperiod").val(kjperiod);
                                $("#editkjcode").val(kjcode);
                                $("#editaward").val(kjmoney);
                                $("#kjstates").val(kstate);
                                
                                $("#editorUserModal").modal();
            
                              
                                $(".jiesuanbox").show();
                            });

                            $("#editUserBtn").unbind().click(function(){
                                _this.editorPeriod();
                            });

                          
                            
                            // 分页
                            var pn = count.pn;
                            var tp = count.tp;
                            var tr = count.tr;
                            $("#countpages").html( tp );
                            $("#countnums").html( tr );
        
                            var pageHtml = getPages(pn,tp, "$.page");

                            $("#paginationSel").html(pageHtml);

                        }
                        else
                        {
                            $("#unticketList").html(_this.nodata);

                        }

                    }
                    else
                    {
                       $.alert(code, desc);
                    }


                }
            });


        },
        editorPeriod:function(){
            var _this = this;
            var gameId = $("#gameid").val();
            var kjperiod = $("#editperiod").val();
            var kjcodes = $("#editkjcode").val();
            var kjaward = $("#editaward").val();
            var kjstate =  $("#kjstates").val();
            var isChecked = $("#checkstatus").prop("checked");
            if( isChecked )
            {
                kjstate = 1;
            }
            $.ajax({
                type:"get",
                url:"/admin/update_period?gameid="+gameId+"&period="+kjperiod+"&state="+kjstate+"&oldvalue="+kjcodes,
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code==1)
                    {
                        var currentpage = _this.pagenum;
                        $("#editUserBtn").popover("show");
                        _this.getReportList(currentpage);
                       setTimeout(function(){
                          $("#editUserBtn").popover("hide");
                          $("#editorUserModal").modal("hide");
                       }, 400);

                    }
                    else
                    {
                       $("#editorUserModal").modal("hide");
                       $.alert(code, desc);

                    }

                }
            });
             

      
          

        }
 
   
  
   
      
      
    };





  new GetPeriodInfo();

}());